#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "FileSystem.h"

#ifdef NDEBUG
#error Cannot test in NDEBUG mode! Please run "./boostrap --enable-debug" 
#endif

int main()
{
	FileSystem_Init();
	FileHandle file;

	/* check Open function */
	assert(false == Open(&file, "/this/is/not/exists/file/hahaha", FM_OpenRead));

	unlink("./newfile");
	assert(false == Open(&file, "./newfile", FM_OpenRead));
	assert(true == Open(&file, "./newfile", FM_OpenRW | FM_Create));

	/* check Write function */
	assert(6 == Write(&file, "preved", 6));
	assert(1 == Write(&file, " ", 1));
	assert(6 == Write(&file, "medved", 6));

	/* check Seek & Read function */
	assert(0 == Seek(&file, 0, SEEK_SET));
	char buf[256];
	
	memset(buf, 0, sizeof(buf));
	assert(13 == Read(&file, buf, sizeof(buf)));
	assert(0 == strcmp("preved medved", buf));
	assert(7 == Seek(&file, 7, SEEK_SET));
	
	memset(buf, 0, sizeof(buf));
	assert(6 == Read(&file, buf, sizeof(buf)));

	/* check PRead && Seek */
	assert(0 == strcmp("medved", buf));
	assert(0 == Seek(&file, 0, SEEK_SET));
	
	memset(buf, 0, sizeof(buf));
	assert(6 == PRead(&file, buf, sizeof(buf), 7));
	assert(0 == strcmp("medved", buf));
	
	memset(buf, 0, sizeof(buf));
	assert(13 == Read(&file, buf, sizeof(buf)));
	assert(0 == strcmp("preved medved", buf));
	
	/* check PWrite function */
	assert(6 == PWrite(&file, "medved", 6, 0));
	assert(6 == PWrite(&file, "preved", 6, 7));


	assert(0 == Seek(&file, 0, SEEK_SET));
	memset(buf, 0, sizeof(buf));
	assert(13 == Read(&file, buf, sizeof(buf)));
	assert(0 == strcmp("medved preved", buf));

	FileSystem_Free();
	return 0;
};

